package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.NoticeListDto;
import com.hualude.roadsign.repository.NoticeAddRepository;
import com.hualude.roadsign.repository.NoticeListRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.NoticeListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by TonyChan on 2017/4/15.
 */
@Service
public class NoticeListServiceImpl implements NoticeListService {
    @Resource
    private NoticeListRepository noticeListRepository;

    @Transactional
    public List<NoticeListDto> getNoticeListHome(Integer userId) throws Exception {
        return noticeListRepository.getNoticeByUserId(userId);
    }

    public NoticeListDto getNoticeByNotceId(Integer userId) throws Exception {
        return noticeListRepository.getNoticeByNotceId(userId);
    }

    public List<NoticeListDto> getNoticeList4DT(Integer start, Integer length, Integer userId, String search) throws Exception {
        List<NoticeListDto> dto4DTs = noticeListRepository.getNoticeList4DT(start, length, userId, search);
        return dto4DTs;
    }

    @Transactional
    public Integer deleteNoticeByUserNoticeId(String userNoticeIds) throws Exception {
        String[] userNoticeArray = userNoticeIds.split(",");
        Integer i;
        for (i = 0; i < userNoticeArray.length; i++) {
            noticeListRepository.deleteNotice(Integer.parseInt(userNoticeArray[i]));
        }
        return i;
    }

    @Transactional
    public Integer updateIsRead(Integer userNoticeId) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return noticeListRepository.updateIsRead(userNoticeId, df.format(new Date()));
    }
}